package main.userInterface.hexislandui;


import main.model.hexisland.tile.TileTree;
import main.model.hexisland.tile.TileType;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;

public class HexTileSampler extends JPanel{

    private boolean isTree = false;
    private double treeHealth = 1;
    private double size  = 20;
    private TileType type = TileType.TREE;

    public void setTreeHealth(double treeHealth){
        this.treeHealth = treeHealth;
        repaint();
    }

    public HexTileSampler(TileType type, double size , double treeHealth){
        isTree = false;
        this. treeHealth = treeHealth ;
        this.size = size;
        this.type = type;
        repaint();

        this.setPreferredSize(new Dimension((int) size, (int) size));
    }


    public HexTileSampler(double size , double treeHealth){
        isTree = true;
        this. treeHealth = treeHealth ;
        this.size = size;
        repaint();

        this.setPreferredSize(new Dimension((int) size, (int) size));

    }


    public void paintComponent(Graphics g){
        if(isTree) paintTree(g);
        else paintTile(g);
    }

    private void paintTree(Graphics g){
        Graphics2D g2 =  (Graphics2D)g;

        g2.setPaint(HexIslandCanvas.TREE_GREEN);
        g2.fill(new Ellipse2D.Double(0, 0 ,size,size));

        double angle = 3.6*(100.0/TileTree.getTreeMax()*treeHealth);
        //System.out.println("Angle is "+angle+" treehealth "+treeHealth+ " max "+TileTree.getTreeMax());
        g2.setPaint(HexIslandCanvas.TREE_BROWN);
        g2.fill(new Arc2D.Double(
                0, 0 ,size,size,
                angle , 360-angle , Arc2D.PIE));

    }


    private void paintTile(Graphics g){
        Graphics2D g2 =  (Graphics2D)g;
        boolean background = true;
        boolean obstacle = true;

        if(background){
            switch(type){
                case GRASS: g2.setPaint(HexIslandCanvas.GRASS); break;
                case TREE: g2.setPaint(HexIslandCanvas.GRASS); break;
                case WATER: g2.setPaint(HexIslandCanvas.WATER);  break;
                default: g2.setPaint(HexIslandCanvas.SAND);  break;
            }
            g2.fill(new RoundRectangle2D.Double(0,0,size, size, size/2,size/2));
        }
        if(obstacle){
            switch(type){
                case TREE:
                    g2.setPaint(HexIslandCanvas.TREE_GREEN);
                    g2.fill(new Ellipse2D.Double(size/6, size/6 ,size-size/3,size-size/3));

                    double angle = 3.6*(100.0/TileTree.getTreeMax()*treeHealth);
                    g2.setPaint(HexIslandCanvas.TREE_BROWN);
                    g2.fill(new Arc2D.Double(
                            size/6, size/6 ,size-size/3,size-size/3,
                            angle , 360-angle , Arc2D.PIE));
                    break;
                case ROCK:
                    g2.setPaint(HexIslandCanvas.ROCK);
                    g2.fill(new Ellipse2D.Double(size/6, size/6 ,size-size/3,size-size/3));
                    break;
            }
        }
    }

}